﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class ScrollRectSnap : MonoBehaviour {
	
	float[] points;
	[Tooltip("how many screens or pages are there within the content (steps)")]
	public int screens = 1;
	int currentscreen = 0;
	int previousscreen = 0;
	public int RealScreen = 0;
	float stepSize;
	
	ScrollRect scroll;
	bool LerpH;
	public float targetH;
	[Tooltip("Snap horizontally")]
	public bool snapInH = true;
	
	bool LerpV;
	float targetV;
	[Tooltip("Snap vertically")]
	public bool snapInV = true;
	
	// Use this for initialization
	void Start ()
	{
		scroll = gameObject.GetComponent<ScrollRect>();
		//scroll.intertia = false;
		
		if(screens > 0)
		{
			points = new float[screens];
			stepSize = 1/(float)(screens-1);
			
			for(int i = 0; i < screens; i++)
			{
				points[i] = i * stepSize;
			}
		}
		else
		{
			points[0] = 0;
		}
	}
	
	void Update()
	{
		if(LerpH)
		{
			previousscreen = currentscreen;
			scroll.horizontalNormalizedPosition = Mathf.Lerp( scroll.horizontalNormalizedPosition, targetH, 50*scroll.elasticity*Time.deltaTime);
			if(Mathf.Approximately(scroll.horizontalNormalizedPosition, targetH)) LerpH = false;            
		}
		if(LerpV)
		{
			scroll.verticalNormalizedPosition = Mathf.Lerp( scroll.verticalNormalizedPosition, targetV, 50*scroll.elasticity*Time.deltaTime);
			if(Mathf.Approximately(scroll.verticalNormalizedPosition, targetV)) LerpV = false;            
		}
		
		
	}
	
	public void DragEnd()
	{
		
		if(scroll.horizontal && snapInH)
		{
			targetH = points[FindNearest(scroll.horizontalNormalizedPosition, points)];
			LerpH = true;
		}
		if(scroll.vertical && snapInV)
		{
			targetH = points[FindNearest(scroll.verticalNormalizedPosition, points)];
			LerpH = true;
		}
		
		currentscreen = Mathf.RoundToInt(targetH * 100);
		
		if (previousscreen < currentscreen)
		{
			RealScreen ++;
		}
		if (previousscreen > currentscreen)
		{
			RealScreen --;
		}

		
	}
	
	public void OnDrag()
	{
		
		LerpH = false;
		LerpV = false;
	}
	
	int FindNearest(float f, float[] array)
	{
		float distance = Mathf.Infinity;
		int output = 0;
		for(int index = 0; index < array.Length; index++)
		{
			if(Mathf.Abs(array[index]-f) < distance)
			{
				distance = Mathf.Abs(array[index]-f);
				output = index;
			}
		}
		return output;    
	}
}
